package bjtu.cit.bookshop.controller;

import bjtu.cit.bookshop.entities.Book;
import bjtu.cit.bookshop.entities.Order;
import bjtu.cit.bookshop.entities.OrderBook;
import bjtu.cit.bookshop.entities.User;
import bjtu.cit.bookshop.service.IBookService;
import bjtu.cit.bookshop.service.IOrderService;
import bjtu.cit.bookshop.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZacharyJia on 2015/9/29.
 */
@Controller
@RequestMapping("/")
public class IndexController extends BaseController {

    @Resource
    IBookService bookService = null;

    @Resource
    IUserService userService = null;

    @Resource
    IOrderService orderService = null;


    /**
     * 首页
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("index.do")
    public String index(ModelMap model, HttpSession session) {
        model.addAttribute("current", "home");

        String str_isLogin = (String)session.getAttribute("isLogin");
        boolean isLogin;
        if (str_isLogin != null && str_isLogin.equals("true")) {
            isLogin = true;
            String loginName = (String)session.getAttribute("loginName");
            if (loginName != null) {
                model.addAttribute("loginName", loginName);
            }
        }
        else {
            isLogin = false;
        }
        model.addAttribute("isLogin", isLogin);

        Book book = new Book();
        book.setType(Book.INDEX_BOOK);
        List<Book> bookList = bookService.getBookByNotNullField(book);
        model.addAttribute("bookList", bookList);

        return "index";
    }


    /**
     * 精品图书页面
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("excellentBook.do")
    public String excellentBook(ModelMap model, HttpSession session) {
        model.addAttribute("current", "excellent");

        String str_isLogin = (String)session.getAttribute("isLogin");
        boolean isLogin;
        if (str_isLogin != null && str_isLogin.equals("true")) {
            isLogin = true;
            String loginName = (String)session.getAttribute("loginName");
            if (loginName != null) {
                model.addAttribute("loginName", loginName);
            }
        }
        else {
            isLogin = false;
        }
        model.addAttribute("isLogin", isLogin);

        Book book = new Book();
        book.setType(Book.EXCELLENT_BOOK);
        List<Book> bookList = bookService.getBookByNotNullField(book);
        model.addAttribute("bookList", bookList);

        return "excellentBook";
    }


    /**
     * 新书上架
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("newBook.do")
    public String newBook(ModelMap model, HttpSession session) {
        model.addAttribute("current", "new");

        String str_isLogin = (String)session.getAttribute("isLogin");
        boolean isLogin;
        if (str_isLogin != null && str_isLogin.equals("true")) {
            isLogin = true;
            String loginName = (String)session.getAttribute("loginName");
            if (loginName != null) {
                model.addAttribute("loginName", loginName);
            }
        }
        else {
            isLogin = false;
        }
        model.addAttribute("isLogin", isLogin);

        Book book = new Book();
        book.setType(Book.NEW_BOOK);
        List<Book> bookList = bookService.getBookByNotNullField(book);
        model.addAttribute("bookList", bookList);

        return "newBook";
    }


    /**
     * 图书详细信息
     * @param session
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("bookDetail.do")
    public String bookDetail(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkLogin(session);
        if (loginName == null) {
            model.addAttribute("isLogin", false);
        }
        else {
            model.addAttribute("isLogin", true);
            model.addAttribute("loginName", loginName);
        }
        String str_id = request.getParameter("id");
        try {
            int id = Integer.parseInt(str_id);
            Book book = bookService.getBookById(id);
            model.addAttribute("book", book);
            return "bookDetail";
        }
        catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("errmsg", "参数错误！");
            return "error";
        }
    }


    @RequestMapping("userInfo.do")
    public String userInfo(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkLogin(session);
        if (loginName == null) {
            return "redirect:/index.do";
        }
        User user = userService.getUserByUserName(loginName);
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("user", user);
        return "userinfoedit";
    }


    /**
     * 显示订单列表
     * @param session
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("MyOrder.do")
    public String order(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkLogin(session);
        if (loginName == null) {
            return "redirect:/login.do";
        }

        String page = request.getParameter("page");

        if (page == null) {
            page = "1";
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "order");

        int userId = (Integer)session.getAttribute("userId");
        List<Order> orderList;
        try {
            orderList = orderService.getOrderListByUserId(userId);
        }
        catch (Exception e){
            e.printStackTrace();
            model.addAttribute("errmsg", "参数错误！");
            return "error";
        }
        model.addAttribute("orderList", orderList);
        return "/order";
    }


    @RequestMapping("orderInfo.do")
    public String orderDetail(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkLogin(session);
        if (loginName == null) {
            return "redirect:/login.do";
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);

        String id = request.getParameter("id");
        try {
            Order order = orderService.getOrderById(Integer.valueOf(id));
            List<OrderBook> books = orderService.getBookListByOrderId(order.getId());
            List<Book> bookList = new ArrayList<Book>();
            List<Integer> numberList = new ArrayList<Integer>();
            for (OrderBook orderBook : books) {
                bookList.add(bookService.getBookById(orderBook.getBookid()));
                numberList.add(orderBook.getNum());
            }

            model.addAttribute("order", order);
            model.addAttribute("bookList", bookList);
            model.addAttribute("numberList", numberList);

            return "orderinfo";
        }
        catch (Exception e) {
            e.printStackTrace();
            return "/admin/badParameter";
        }
    }


    /**
     * 内部类，用于封装订单内的图书列表
     */
    class OrderBookDetail
    {
        private String book;

        public OrderBook getOrderBook() {
            return orderBook;
        }

        public void setOrderBook(OrderBook orderBook) {
            this.orderBook = orderBook;
        }

        private OrderBook orderBook;

        public String getBook() {
            return book;
        }

        public void setBook(String book) {
            this.book = book;
        }
    }



}
